package com.kotlin.network.retrofit.http;

import com.kotlin.network.ErrorCode;


public class ApiException extends RuntimeException {


    //public ApiException(int resultCode) {
    //    this(getApiExceptionMessage(resultCode));
    //}



    private int errorCode;

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public ApiException(int resultCode,String errorMsg) {
        this(getApiExceptionMessage(resultCode,errorMsg));
        setErrorCode(resultCode);
    }

    public ApiException(String detailMessage) {
        super(detailMessage);
    }



    /**
     * 由于服务器传递过来的错误信息直接给用户看的话，用户未必能够理解
     * 需要根据错误码对错误信息进行一个转换，在显示给用户
     * @param code
     * @return
     */
    //private static String getApiExceptionMessage(int code){
    //    String message = "";
    //    switch (code) {
    //        case USER_NOT_EXIST:
    //            message = "该用户不存在";
    //            break;
    //        case WRONG_PASSWORD:
    //            message = "密码错误";
    //            break;
    //        default:
    //            message = "未知错误";
    //
    //    }
    //    return message;
    //}


    private static String getApiExceptionMessage(int code,String errorMsg){

        String message = errorMsg;
        switch (code) {
            case ErrorCode.Fail:
                message = errorMsg;
                break;
            case ErrorCode.TokenTimeOutError:
                message = "您的用户信息已过期，请重新登录";
                break;
            //case ErrorCode.UnknowError:
            //    message = "未知错误";
            default:
                //message = "未知错误";

        }
        return message;
    }
}

